<?php

function list_complain(){
my_set_code_js('
		function confirmDelete( i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+i;
		}
		return;	}
	');
	$header = array(
		'NOCP'=>array('style'=>'text-align:center;border-bottom:2px solid;width:7%'), 
		'Nik'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'Nama'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:36%'),  
		'Tanggal'=>array('style'=>'border-bottom:2px solid;width:10%'),  	
		'Solved'=>array('style'=>'border-bottom:2px solid;width:7%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	if(isset($_GET['key'])){
		$query = "
			SELECT  a.id , a.datetime_added AS datetime_added_masalah , 
				a.karyawan_id , a.ada_solusi FROM helpdesk a 
			INNER JOIN karyawan b ON b.karyawan_id = a.karyawan_id  
			INNER JOIN biodata c ON b.biodata_id = c.biodata_id 
			WHERE   a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN ( select karyawan_id FROM pemberhentian ) 	
			AND ( c.nama LIKE '%{$_GET['key']}%' OR b.nik ='{$_GET['key']}' ) 
			ORDER BY a.datetime_added DESC 
		";
	}else{	
		$query = "SELECT a.id , a.datetime_added AS datetime_added_masalah , 
				a.karyawan_id , a.ada_solusi FROM helpdesk a 
			INNER JOIN karyawan b ON b.karyawan_id = a.karyawan_id   
			WHERE   a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN ( select karyawan_id FROM pemberhentian ) 
			ORDER BY a.datetime_added DESC ";
	}
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =15;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 15; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$jabatan =  loaddata_jabatan($karyawan['jabatan_id']);  
		
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=detail&id='.$ey['id'],
			'title'=>'Detail','rel'=>'facebox'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$cetakproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=cetak&id='.$ey['id'],
			'title'=>'Cetak SPD'
		);
		$cetak_button = button_icon( 'b_print.png' , $cetakproperty  );
		
		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'nomor'=>position_text_align ( sprintf('%06d' ,$ey['id']), 'center'),
			'nik'=>position_text_align( $karyawan['nik'], 'center'),
			'nama'=> $karyawan['nama_gelar'],    	
			'jabatan'=> $jabatan['nama_jabatan'],    	
			'tanggal_pengajuan'=>date('d-m-Y',strtotime($ey['datetime_added_masalah'])),			 	 	 	 	 	 	
			'solve'=>( ($ey['ada_solusi'] == '1') ? 'Sudah' : 'Belum' ), 
			'operasi'=> position_text_align( /* $detail_button .' '.*/$delete_button .' '.$cetak_button, 'right'),  
		);
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(  
		'Create' => array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=create\' });'),
	//	'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&key='.$_GET['key'].'\';'),
	//	'XL Batas' => array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=excel_range\' });'),
	);	
	
	$printmenu = additional_menu_on_list( $menuontop, $search);
	return $printmenu .table_builder($header , $datas ,  15 ,false , $paging  );	
}


function form_complain($id=0){
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/js/calendar/calendarDateInput.js'
		)
	);
	my_set_file_css(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);
	
	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
				//	autoFill:true
				}
			);
		});
	
		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			function(data){
				if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
			} 
			});
		}
	');
	$view = form_header( "Complain Detail" , "Complain Detail"  );
	
 	$fields = my_get_data_by_id('helpdesk',	'id', $id);
	$karyawan = loaddata_karyawan( $fields['karyawan_id'] );
	$nama_karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan'] ) ? $_POST['nama_karyawan'] : $karyawan['nik'].($fields ? '/' : '').$karyawan['nama']  ),
		'id'=>'nama_karyawan' , 
		'type'=>'text',
		'size'=>'45' ,
		//'onchange'=>'javascript:checkName(this.value)'
	);
	
	$form_field_nama_karyawan = form_dynamic($nama_karyawan);
	$view .= form_field_display( $form_field_nama_karyawan , "Karyawan "  );
 
	$deskripsi_masalah = array(
			'name'=>'deskripsi_masalah',
			'value'=>(isset($_POST['deskripsi_masalah'])? $_POST['deskripsi_masalah'] : $fields['deskripsi_masalah']),
			'id'=>'deskripsi_masalah' ,
			'cols'=>44,
			'rows'=>8
	);
	$form_deskripsi_masalah = form_textarea($deskripsi_masalah);
	$view .= form_field_display( $form_deskripsi_masalah  , "Permasalahan"  );
	
	$solusi = array(
			'name'=>'solusi',
			'value'=>(isset($_POST['solusi'])? $_POST['solusi'] : $fields['solusi']),
			'id'=>'solusi' , 
			'cols'=>44,
			'rows'=>10
	);
	$form_solusi = form_textarea($solusi);
	$view .= form_field_display( $form_solusi  , "Solusi yang mungkin dilakukan"  );
	
	if(isset($fields)){
		if($fields['ada_solusi'] == '1'){
			$ada_solusi = array(
					'name'=>'ada_solusi',
					'id'=>'ada_solusi' ,
					'checked'=> 'checked'
			);
		}else{
			$ada_solusi = array(
					'name'=>'ada_solusi',
					'id'=>'ada_solusi' ,  
			); 
		}
	}else{
		$ada_solusi = array(
				'name'=>'ada_solusi',
				'id'=>'ada_solusi' ,  
		); 
	}
	$form_solusi = form_checkbox($ada_solusi);
	$view .= form_field_display( $form_solusi .'Masalah dapat diselesaikan'  , ""  );

	$submit = array(
		'value' =>  ' Simpan ' ,
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	 
	
	$view .= form_field_display( $form_submit    , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view; 
}

function insert_complain($id){
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']); 
	$ada_solusi = isset($_POST['ada_solusi']) ? 1: 0;
	
	if($id > 0){
		$datas = array(
			'karyawan_id' => my_type_data_int($karyawan_id),
			'deskripsi_masalah' => my_type_data_str($_POST['deskripsi_masalah']),
			'solusi' => my_type_data_str($_POST['solusi']),
			'ada_solusi' => my_type_data_str($ada_solusi)
			);
		return my_update_record('helpdesk','id', $id , $datas);
	}
	$datas = array(
		'karyawan_id' => my_type_data_int($karyawan_id),
		'deskripsi_masalah' => my_type_data_str($_POST['deskripsi_masalah']),
		'solusi' => my_type_data_str($_POST['solusi']),
		'ada_solusi' => my_type_data_str($ada_solusi),
		'datetime_added' => my_type_data_function('NOW()'),
	);
	return my_insert_record('helpdesk',  $datas);
}


function form_complain_validate(){
	$errsubmit = false;
	$err = array();
	
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);  
	if ((int) $karyawan_id == 0){
		$errsubmit = true;
		$err[] = "NIK Tidak dikenal";
	} 
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function delete_complain($id){
	return my_query("DELETE FROM helpdesk WHERE id = {$id} ");
}